package com.ids.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.ids.util.DbManager;
import com.ids.util.Utlity;

public class AdminPasswordDao {
	private static AdminPasswordDao instance = new AdminPasswordDao();

	public static AdminPasswordDao getInstance() {
		return instance;
	}

	public String verifyAdmin(String empCode, String password) {
		String status = "error";
		DbManager dbManager = new DbManager();
		Connection conn = dbManager.getConnection();
		ResultSet adminAuthRS = null;
		String sql = "select * from logininfo where empCode=? and password=? ";
		try {
			PreparedStatement preStmt = conn.prepareStatement(sql);
			preStmt.setString(1, empCode);
			preStmt.setString(2, Utlity.MD5(password));
			adminAuthRS = preStmt.executeQuery();
			if (adminAuthRS.next()) {
				status = "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}

	public String updateAdminPassword(String empCode, String oldpassword,String newpassword) {
		String status = "error";
		DbManager dbManager = new DbManager();
		Connection conn = dbManager.getConnection();
		String sql = "update logininfo set password = ? where empCode=? and password=? ";
		try {
			PreparedStatement preStmt = conn.prepareStatement(sql);
			preStmt.setString(1, Utlity.MD5(newpassword));
			preStmt.setString(2, empCode);
			preStmt.setString(3, Utlity.MD5(oldpassword));
			preStmt.execute();
			status = "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}
}
